package com.ruoyi.utils;

import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;

public class OcrUtils {


    public static String XckOcr(String url,String accessKeyId,String accessKeySecret)  {

        com.aliyun.ocr_api20210707.Client client = null;
        try {
            client = OcrUtils.createClient(accessKeyId, accessKeySecret);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        com.aliyun.ocr_api20210707.models.RecognizeTravelCardRequest recognizeTravelCardRequest = new com.aliyun.ocr_api20210707.models.RecognizeTravelCardRequest()
                .setUrl(url);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.ocr_api20210707.models.RecognizeTravelCardResponse resp = null;
        try {
            resp = client.recognizeTravelCardWithOptions(recognizeTravelCardRequest, runtime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("文字识别返回结果："+(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp))));

        try {
            // 复制代码运行请自行打印 API 的返回值
            client.recognizeTravelCardWithOptions(recognizeTravelCardRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return  com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp));

    }




    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }














}
